package com.wzq.design.business.system.fun.controller;

import com.wzq.design.base.controller.CommController;
import com.wzq.design.base.dto.QueryByIdRequest;
import com.wzq.design.base.dto.RemoveRequest;
import com.wzq.design.business.system.fun.dto.FunRequest;
import com.wzq.design.business.system.fun.dto.SysFunDto;
import com.wzq.design.business.system.fun.service.FunService;
import com.wzq.ssm.parent.base.dto.JsonResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author wangzhiqiang
 * @since 2023/5/8 15:07
 */
@RestController
@RequestMapping("/sys/fun")
public class FunController extends CommController {
    private final FunService funService;

    public FunController(FunService funService) {
        this.funService = funService;
    }


    @PostMapping("/list")
    public JsonResult<List<SysFunDto>> list(@RequestBody @Valid FunRequest funRequest) {
        return JsonResult.success("查询成功", funService.list(funRequest));
    }
    @GetMapping("/listTree")
    public JsonResult<List<SysFunDto>> listTree(@Valid FunRequest request) {
        return JsonResult.success("查询成功", funService.listTree(request));
    }

    @PostMapping("/delete")
    public JsonResult<?> delete(@RequestBody RemoveRequest request) {
        funService.deleteByIds(request.getIds());
        return JsonResult.success("删除成功");
    }

    @PostMapping("/save")
    public JsonResult<SysFunDto> save(@RequestBody SysFunDto request) {
        if ("0".equals(request.getParentId())) {
            request.setParentId("");
        }
        return JsonResult.success("保存成功", funService.save(request));
    }


    @GetMapping("/findById")
    public JsonResult<SysFunDto> findById(QueryByIdRequest request) {
        return JsonResult.success("保存成功", funService.findById(request));
    }
}
